<?php


require ROOT_PATH.'/Helper/admin_abstract.php';

class Action extends admin_abstract
{
    function set_tags($user_id)
    {
        if (!$user_info = _model('user')->getInfoById($user_id)) {
            //@todo check msg
            $this->msg('user not exists');
        }

        if (Request::isPost()) {
            $tags = _POST('tags', array());
            $tags = array_filter($tags);
            _model('user')->update(array('id'=>$user_id), array('tags' => implode('|', $tags)));
            $this->msg('Set tags was successful!', "admin/admin_manage/set_tags/$user_id.html");
        } else {
            $tags = array();
            $data = _model('tags')->getList('ORDER BY view_order ASC');
            foreach ($data as $key => $val) {
                $tags[$val['id']] = $val;
            }
            $user_info['tags'] = array_filter(explode('|', $user_info['tags']));
            $this->view->assign('tags', $tags);
            $this->view->assign('info', $user_info);
            $this->view->display('admin/admin_set_tags.html');
        }

    }

    function delete($user_id)
    {
        if ($this->info['user_group']['id'] > 2) {
            $this->msg('no permistion');
        }

        if (!$user_info = _model('user')->getInfoById($user_id)) {
            //@todo check msg
            $this->msg('user not exists');
        }
        _model('user')->delete(array('id'=>$user_id));
        $this->redirect();
    }

    function edit($user_id)
    {
        if ($this->info['user_group']['id'] > 2) {
            # code...show msg exit
        }

        if (!$user_info = _model('user')->getInfoById($user_id)) {
            //@todo check msg
            $this->msg('user not exists');
        }
    
        if (Request::isPost()) {
            $info = _POST('info', array());
            if (empty($info['user_name']) || empty($info['email'])) {
                $this->msg('Operation Failed, bad request, Back to continue');
            }
            if ($info['user_name'] != $user_info['user_name']) {
                if (_model('user')->getInfoByName($info['user_name'])) {
                    $this->msg('user exists');
                }
            } else {
                unset($info['user_name']);
            }
            if (empty($info['password'])) {
                unset($info['password']);
            } else {
                $info['password'] = md5($info['password']);
            }
            
            if (!empty($info['full_name'])) {
                $check = _model('user')->read(array('full_name'=>$info['full_name']));
                if ($check && $check['id'] != $user_id) {
                    $this->msg('full name exists');
                }
            }

            $check_name = _model('user')->read(array('user_name'=>$info['user_name']));
            if ($check_name && $check_name['id'] != $user_id) {
                $this->msg('user name exists');
            }
            
            
            _model('user')->update_info($user_id, $info);
            $this->redirect('admin/admin_manage.html');
        } else {
            $admin_group = $this->get_admin_group();
            if ($this->info['user_group']['id'] == 2) {
                unset($admin_group[1], $admin_group[2]);
            }
            $this->view->assign('admin_group', $admin_group);
            $this->view->assign('info', $user_info);
            $this->view->display('admin/user_manage_edit.html');
        }
    }

    function add()
    {
        if ($this->info['user_group']['id'] > 2) {
            $this->msg('no permistion');
        }

        if (Request::isPost()) {
            $info = _POST('info', array());
            if (empty($info['user_name']) || empty($info['password']) || empty($info['email']) || empty($info['full_name'])) {
                $this->msg('Operation Failed, bad request, Back to continue');
            }
            if (_model('user')->getInfoByName($info['user_name'])) {
                $this->msg('user exists');
            }
            $info['add_user'] = $this->info['id'];
            if (!empty($info['full_name'])) {
                $check = _model('user')->read(array('full_name'=>$info['full_name']));
                if ($check) {
                    $this->msg('full name exists');
                }
            }
            
            _model('user')->register($info);
            $this->redirect('admin/admin_manage.html');
        } else {
            $admin_group = $this->get_admin_group();
            if ($this->info['user_group']['id'] == 2) {
                unset($admin_group[1], $admin_group[2]);
            }
            $this->view->assign('admin_group', $admin_group);
            $this->view->display('admin/user_manage_add.html');
        }
    }

    function index()
    {
        if ($this->info['user_group']['id'] == 1) {
            $data = _model('user')->getList('WHERE user_group > 0', new Pager(20));
        } else {
            $data = _model('user')->getList(array('add_user'=>$this->info['id']), 'AND user_group > 0', new Pager(20));
        }

        $this->view->assign($data);
        $this->view->assign('admin_group', $this->get_admin_group());
        $this->view->display('admin/user_manage.html');
    }
}

?>